package cn.demoxy.service.impl;

import cn.demoxy.mapper.DemoLogMapper;
import cn.demoxy.model.dto.DemoLogDTO;
import cn.demoxy.model.entity.DemoLog;
import cn.demoxy.model.entity.DemoTenant;
import cn.demoxy.service.IDemoLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import static cn.demoxy.constant.CommonConstant.LIKE_COLUMN;
import static cn.demoxy.constant.CommonConstant.LOG_TYPE_LOGIN;

/**
 * @auther demo
 * @date 2022/5/18 20:17
 * 日志表业务实现
 */
@Service
public class DemoLogServiceImpl extends ServiceImpl<DemoLogMapper, DemoLog> implements IDemoLogService {
    @Override
    public IPage<DemoLog> findAllLogByKeyword(DemoLogDTO logDTO) {
        //创建page对象，传递当前页，每页记录数
        Page<DemoLog> page = new Page<>(logDTO.getCurrent(), logDTO.getSize());
        //构建条件
        QueryWrapper<DemoLog> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(logDTO.getLogType())) {
            if (logDTO.getLogType().equals(LOG_TYPE_LOGIN)) {
                wrapper.like("title", LIKE_COLUMN);
            } else {
                wrapper.notLike("title", LIKE_COLUMN);
            }
        }
        //根据日志标题名称查询
        if (StringUtils.hasText(logDTO.getTitle())) {
            wrapper.like("title", logDTO.getTitle());
        }
        //根据日志操作人查询
        if (StringUtils.hasText(logDTO.getUser())) {
            wrapper.like("user", logDTO.getUser());
        }
        //根据日志业务类型查询
        if (StringUtils.hasText(logDTO.getBusinessType())) {
            wrapper.like("business_type", logDTO.getBusinessType());
        }
        //根据日期范围查询日志
        if (StringUtils.hasText(logDTO.getStartDate())&&StringUtils.hasText(logDTO.getEndDate())) {
            wrapper.ge("create_time",logDTO.getStartDate()).le("create_time",logDTO.getEndDate());
        }
        //调用方法实现分页查询
        Page<DemoLog> logPages = baseMapper.selectPage(page,wrapper.select().orderByDesc("create_time"));
        //返回结果
        return logPages;
    }
}
